package com.shangcheng.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shangcheng.goods.constant.BrandConstant;
import com.shangcheng.goods.dao.BrandMapper;
import com.shangcheng.goods.service.BrandService;
import com.zhouyangyang.goods.pojo.Brand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.shangcheng.goods.service.impl
 * @ClassName: BrandServiceImpl
 * @Author: zhouyangyang
 * @Date: 2020/7/26 0:05
 * @Description:
 */
@Service
@Slf4j
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    /**
     * 查询所有品牌
     * @return list
     */
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    /**
     * 根据id查询
     * @param id 品牌id
     * @return brand
     */
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增品牌
     * @param brand 品牌
     */
    @Override
    @Transactional
    public void addBrand(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * 删除品牌
     * @param id 品牌id
     */
    @Override
    @Transactional
    public void deleteBrand(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 多条件搜索品牌
     * @param searchMap 搜索条件
     * @return brandList
     */
    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = getExampleMethod(searchMap);
        return brandMapper.selectByExample(example);
    }

    /**
     * 分页查询
     * @param page 当前页
     * @param size 显示条数
     * @return
     */
    @Override
    public Page<Brand> findToPage(int page, int size) {
        PageHelper.startPage(page,size);
        return (Page<Brand>) brandMapper.selectAll();
    }

    /**
     * 分页条件查询
     * @param searchMap 查询条件
     * @param page 当前页
     * @param size 每页显示条数
     * @return brandList
     */
    @Override
    public Page<Brand> findToPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = getExampleMethod(searchMap);
        return (Page<Brand>) brandMapper.selectByExample(example);
    }

    /**
     * 根据分类名称查询品牌列表
     * @param name
     * @return
     */
    @Override
    public List<Map> findListByCateGoryName(String name) {
        if(name.isEmpty()){
            log.error("BrandServiceImpl findList param is null");
            throw new NullPointerException();
        }
        return brandMapper.findListByCategoryName(name);
    }

    //构建条件组合方法
    private Example getExampleMethod(Map<String, Object> searchMap) {
        if (searchMap.isEmpty()) {
            log.error("BrandServiceImpl findList param is null");
            throw new NullPointerException();
        }
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        //品牌名称
        if (searchMap.get(BrandConstant.BRAND_NAME) != null && !"".equals(searchMap.get(BrandConstant.BRAND_NAME))) {
            criteria.andLike(BrandConstant.BRAND_NAME, "%" + searchMap.get(BrandConstant.BRAND_NAME) + "%");
        }
        //拼盘首字母
        if (searchMap.get(BrandConstant.BRAND_LETTER) != null && !"".equals(searchMap.get(BrandConstant.BRAND_LETTER))) {
            criteria.andEqualTo(BrandConstant.BRAND_LETTER, searchMap.get(BrandConstant.BRAND_LETTER));
        }
        return example;
    }

    /**
     * 修改品牌
     * @param brand 品牌
     */
    @Override
    @Transactional
    public void upDate(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }
}
